package com.wushi.lenovo.asia5b.app_ui.good.fragment;

import android.text.TextUtils;
import android.view.View;

import com.mvp.common.LocalManageUtil;
import com.mvp.helper.WCallBack;
import com.mvp.utils.DecimalUtil;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemModel;
import com.wushi.lenovo.asia5b.app_ui.good.activity.OrderPackActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.ShopCarNoticeDialog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangyuncai on 2020/3/4.
 * 方便维护
 * 待审核
 */
public class OrderVeriFragment extends OrderItemFragment {

    @Override
    protected void initEvent() {
        super.initEvent();
        selectGoods2(null);
        tv_order_expan.setOnClickListener(v -> {
            tv_order_expan.setSelected(!tv_order_expan.isSelected());
            openBottomView();
        });
        //去打包
        tv_order_pack.setOnClickListener(v -> {
            startOrderPackActivity();
        });
        iv_order_totalvolweight_icon.setOnClickListener(v -> {
            if (TextUtils.equals(LocalManageUtil.getAppLanguage(), "cn")) {
                ToastUtil.showLongToast(getString(R.string.parcel_note_cn));
            } else {
                ToastUtil.showLongToast(getString(R.string.parcel_note_en));
            }
        });
    }

    boolean hasMingan = false;

    public void startOrderPackActivity() {
        hasMingan = false;
        List<OrderGoodBean> goodBeans = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();
        for (OrderItemModel model : mDatas) {
            if (model.getOrder_parcel() != null) {
                for (OrderGoodBean orderGoodBean : model.getOrder_parcel()) {
                    if (orderGoodBean.isChecked) {
                        goodBeans.add(orderGoodBean);
                        idList.add(orderGoodBean.getId());
                        if (!hasMingan && orderGoodBean.isMingan()) {
                            hasMingan = true;
                        }
                    }
                }
            }

        }
        if (idList.size() <= 0) {
            return;
        }
        if (hasMingan) {
            ShopCarNoticeDialog noticeDialog = new ShopCarNoticeDialog(getContext(),
                    getString(R.string.orderitem_mingannotice_title),
                    "");
            noticeDialog.setCallBack(new WCallBack<Boolean>() {
                @Override
                public void callback(Boolean aBoolean) {
                    if (aBoolean) {
                        OrderPackActivity.startOrderPackActivity(getContext(), idList, (Serializable) goodBeans);
                    }
                }
            });
            noticeDialog.show();
        } else {
            OrderPackActivity.startOrderPackActivity(getContext(), idList, (Serializable) goodBeans);
        }
    }

    @Override
    public void selectGoods2(OrderGoodBean bean) {
        String totalactualweight = "0.0";
        String totalvolweight = "0.0";
        String totalm3 = "0.0";
        int selectSum = 0;
        for (OrderItemModel model : mDatas) {
            if (model.getOrder_parcel() != null) {
                for (OrderGoodBean orderGoodBean : model.getOrder_parcel()) {
                    if (orderGoodBean.isChecked) {
                        selectSum++;
                        totalactualweight = DecimalUtil.add(totalactualweight, orderGoodBean.getWeight());
                        totalvolweight = DecimalUtil.add(totalvolweight, orderGoodBean.getVolume_weight());
                        totalm3 = DecimalUtil.add(totalm3, orderGoodBean.getM3_weight());
                    }
                }
            }

        }
        //总实际重量
        tv_order_totalactualweight.setText(String.format("%s KG", totalactualweight));

        //总体积重量
        tv_order_totalvolweight.setText(String.format("%s KG", totalvolweight));

        //总立方米
        tv_order_totalm3.setText(String.format("%s M³", totalm3));

    }

    @Override
    protected void getOrders(int refresh) {
        super.getOrders(refresh);
    }

    @Override
    public void getOrdersResult(List<OrderItemModel> list, int page, int sumPage) {
        super.getOrdersResult(list, page, sumPage);
        ll_order_bottom.setVisibility(mDatas.size() == 0 ? View.GONE : View.VISIBLE);
    }

    /**
     * 打开底部菜单
     */
    private void openBottomView() {
        cons_order_detail_vessel.setVisibility(tv_order_expan.isSelected() ? View.VISIBLE : View.GONE);
    }

}
